package com.zycfc.zsf.boot.redis.command;

import org.springframework.data.redis.connection.jedis.*;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.connection.*;
import java.util.*;
import redis.clients.jedis.*;
import org.slf4j.*;

public class JedisBinaryCommand implements RedisBinaryCommand
{
    private static final Logger log;
    private final JedisConnectionFactory redisConnectionFactory;
    
    public JedisBinaryCommand(final JedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }
    
    private RedisConnection getRedisConnection() {
        final RedisConnection redisConnection = RedisConnectionUtils.getConnection((RedisConnectionFactory)this.redisConnectionFactory);
        return redisConnection;
    }
    
    private void releaseConnection(final RedisConnection redisConnection) {
        RedisConnectionUtils.releaseConnection(redisConnection, (RedisConnectionFactory)this.redisConnectionFactory);
    }
    
    @Override
    public Boolean exists(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).exists(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public String set(final byte[] key, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).set(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public String setex(final byte[] key, final int expire, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).setex(key, expire, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long setnx(final byte[] key, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).setnx(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] get(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).get(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] getSet(final byte[] key, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).getSet(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long del(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).del(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long expire(final byte[] key, final int expire) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).expire(key, expire);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long incr(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).incr(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long incrBy(final byte[] key, final int increment) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).incrBy(key, (long)increment);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long decr(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).decr(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long decrBy(final byte[] key, final int decrement) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).decrBy(key, (long)decrement);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Boolean hexists(final byte[] key, final byte[] field) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hexists(key, field);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long hset(final byte[] key, final byte[] field, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hset(key, field, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long hsetnx(final byte[] key, final byte[] field, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hsetnx(key, field, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public String hmset(final byte[] key, final Map<byte[], byte[]> fieldValues) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hmset(key, (Map)fieldValues);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] hget(final byte[] key, final byte[] field) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hget(key, field);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Map<byte[], byte[]> hgetAll(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Map<byte[], byte[]>)((Jedis)redisConnection.getNativeConnection()).hgetAll(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public List<byte[]> hmget(final byte[] key, final byte[]... field) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (List<byte[]>)((Jedis)redisConnection.getNativeConnection()).hmget(key, field);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long hdel(final byte[] key, final byte[]... field) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hdel(key, field);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long hincrBy(final byte[] key, final byte[] field, final int increment) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).hincrBy(key, field, (long)increment);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] lpop(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).lpop(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] rpop(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).rpop(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long lpush(final byte[] key, final byte[]... value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).lpush(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long lpushx(final byte[] key, final byte[]... value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).lpushx(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long rpush(final byte[] key, final byte[]... value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).rpush(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long rpushx(final byte[] key, final byte[]... value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).rpushx(key, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] lindex(final byte[] key, final int index) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).lindex(key, (long)index);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long linsertAfter(final byte[] key, final byte[] pivot, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).linsert(key, BinaryClient.LIST_POSITION.AFTER, pivot, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long linsertBefore(final byte[] key, final byte[] pivot, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).linsert(key, BinaryClient.LIST_POSITION.BEFORE, pivot, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long llen(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).llen(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public List<byte[]> lrange(final byte[] key, final int start, final int stop) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (List<byte[]>)((Jedis)redisConnection.getNativeConnection()).lrange(key, (long)start, (long)stop);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long lrem(final byte[] key, final int count, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).lrem(key, (long)count, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public String lset(final byte[] key, final int index, final byte[] value) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).lset(key, (long)index, value);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public String ltrim(final byte[] key, final int start, final int stop) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).ltrim(key, (long)start, (long)stop);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long zadd(final byte[] key, final double score, final byte[] member) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).zadd(key, score, member);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long zcount(final byte[] key, final double min, final double max) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).zcount(key, min, max);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Set<byte[]> zrange(final byte[] key, final long start, final long end) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Set<byte[]>)((Jedis)redisConnection.getNativeConnection()).zrange(key, start, end);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Set<Tuple> zrangeWithScores(final byte[] key, final long start, final long end) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Set<Tuple>)((Jedis)redisConnection.getNativeConnection()).zrangeWithScores(key, start, end);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Set<byte[]> zrangeByScore(final byte[] key, final double start, final double end) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Set<byte[]>)((Jedis)redisConnection.getNativeConnection()).zrangeByScore(key, start, end);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Set<Tuple> zrangeByScoreWithScores(final byte[] key, final double start, final double stop) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Set<Tuple>)((Jedis)redisConnection.getNativeConnection()).zrangeByScoreWithScores(key, start, stop);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long zrem(final byte[] key, final byte[]... member) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).zrem(key, member);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Object eval(final byte[] script, final List<byte[]> keys, final List<byte[]> args) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).eval(script, (List)keys, (List)args);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Object evalsha(final byte[] sha1, final List<byte[]> keys, final List<byte[]> args) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).evalsha(sha1, (List)keys, (List)args);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] scriptLoad(final byte[] script, final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).scriptLoad(script);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long sadd(final byte[] key, final byte[]... member) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).sadd(key, member);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long scard(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).scard(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public boolean sismember(final byte[] key, final byte[] member) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).sismember(key, member);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Set<byte[]> smembers(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Set<byte[]>)((Jedis)redisConnection.getNativeConnection()).smembers(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] spop(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).spop(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public List<byte[]> srandmember(final byte[] key, final int count) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (List<byte[]>)((Jedis)redisConnection.getNativeConnection()).srandmember(key, count);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public byte[] srandmember(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).srandmember(key);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long srem(final byte[] key, final byte[]... member) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).srem(key, member);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Set<byte[]> keys(final byte[] patternKey) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return (Set<byte[]>)((Jedis)redisConnection.getNativeConnection()).keys(patternKey);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public String setNxPx(final byte[] key, final byte[] value, final byte[] nxxx, final byte[] expx, final long expireTime) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).set(key, value, nxxx, expx, expireTime);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
    }
    
    @Override
    public Long ttl(final byte[] key) {
        RedisConnection redisConnection = null;
        try {
            redisConnection = this.getRedisConnection();
            return ((Jedis)redisConnection.getNativeConnection()).ttl(key);
        }
        catch (Throwable e) {
            JedisBinaryCommand.log.error("redis ttl key:{} Exception:", (Object)new String(key), (Object)e);
        }
        finally {
            this.releaseConnection(redisConnection);
        }
        return null;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)JedisBinaryCommand.class);
    }
}
